<?php


namespace app\store\controller;

use app\common\model\Help;
use library\Controller;
use library\tools\Data;
use think\Db;
/**
 * 订单管理
 * Class Member
 * @package app\store\controller
 */
class OrderPay extends Controller
{
    /**
     * 绑定数据表
     * @var string
     */
    protected $table = 'StoreOrderPay';

    /**
     * 订单列表
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function index()
    {
        $this->title = '订单列表';

        $order = "id desc";

        $query = $this->_query($this->table)->like('user_id')->equal("status");
        $query->dateBetween('create_at')->order($order)->page();
    }

    /**
     * 数据列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _index_page_filter(&$data)
    {
        foreach($data as $k =>&$v){
            $v['user'] = Db::table("store_member")->where(["id"=>$v['user_id']])->find();
            if($v['status']==0){ $v['status_name'] = '<span style="color: red">已取消</span>'; }
            if($v['status']==1){ $v['status_name'] = '<span style="color: goldenrod">待付款</span>'; }
            if($v['status']==2){ $v['status_name'] = '<span style="color: green">进行中</span>'; }
            if($v['status']==3){ $v['status_name'] = '<span style="color: darkorchid">已完成</span>'; }

            $v['order'] = json_decode($v['order_json'],true);
            foreach ($v['order'] as $kk =>&$vv) {
                $vv['package'] = Db::table("store_package")->where(["id" => $vv['package_id']])->where(["status" => 1, 'is_deleted' => 0])->find();
                $vv['package']['report_image'] = Db::table("store_order_report")->where(["order_id"=>$v['id'],"package_id"=>$vv['package_id']])->value('report_image');
                $vv['project'] = Db::table("store_goods_cate")->where("id", "in", $vv['project_id'])->select();
            }
        }
    }

    /**
     * 修改状态
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function edit_ziduan(){
        $id = $_POST['id']??'';
        $ziduan = $_POST['ziduan']??'';
        $val = $_POST['val']??'';
        Db::query("update store_order_pay set $ziduan='$val' where id=$id");
    }


    /**
     * 检测报告列表
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function report()
    {
        $this->title = '检测报告列表';
        $query = $this->_query("store_order_report")->equal("order_id");
        $query->dateBetween('create_at')->order("id asc")->page();

    }

    /**
     * 数据列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _report_page_filter(&$data)
    {
        foreach($data as $k =>&$v){
            $v['package'] = Db::table("store_package")->where(["id" => $v['package_id']])->where(["status" => 1, 'is_deleted' => 0])->find();
            $v['project'] = Db::table("store_goods_cate")->where("id", "in", $v['project_id'])->select();
        }
    }

    public function files(){
        $id = $_POST['id'];
        $path = "static/uploads/report/";

        $help = new Help();

        $filename = 'image';
        $picname = $_FILES[$filename]['name'];

        if ($picname != "") {
            $type = $help->getSuffix($picname);
            if ($type != 'jpg' && $type != 'gif' && $type != 'png' && $type != 'jpeg' && $type != 'JPG' && $type != 'PNG' && $type != 'JPEG') {
                $array = array(
                    'name' => '格式错误,请上传jpg,gif,png,jpeg',
                    'pic' => '',
                    'size' => '',
                    'pic_path' => ''
                );
                return json_encode($array);
            }
            $type = '.' . $type;
            $md5 = md5(uniqid());
            $pics = $md5 . $type;
            $path = $help->create_dir($path);
            $pic_path = strtr($path, array('./' => '')) . $pics;
            move_uploaded_file($_FILES[$filename]['tmp_name'], $pic_path);

            Db::table("store_order_report")->where("id=$id")->update(['report_image'=>sysconf('realm_name') . '/' . $pic_path,'file_time'=>date('Y-m-d H:i:s')]);
        }
    }

}